package org.shopen.puremvc.view
{
	import flash.display.DisplayObject;
	
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.shopen.puremvc.ApplicationConstants;
	import org.shopen.puremvc.model.vo.ProjectInfo;
	import org.shopen.puremvc.view.components.dialogs.CreateWindow;
	import org.shopen.puremvc.view.components.dialogs.events.CreateDialogEvent;
	import org.shopen.puremvc.view.components.dialogs.events.DialogEvent;

	public class CreateWindowMediator extends Mediator
	{
		public static const NAME : String = "CreateWindowMediator";
		
		protected var createWindow : CreateWindow = null;
		
		protected function get application() : DisplayObject
		{
			var result : DisplayObject = viewComponent as DisplayObject;
			return result;
		}
		
		public function CreateWindowMediator(viewComponent:Object=null)
		{
			super(CreateWindowMediator.NAME, viewComponent);
		}
		
		override public function getMediatorName():String
		{
			return CreateWindowMediator.NAME;
		}
		
		override public function onRemove():void
		{
			removeCreateWindow();
		}
		
		override public function listNotificationInterests():Array
		{
			return [ApplicationConstants.SHOW_CREATING_WINDOW,
					ApplicationConstants.HIDE_CREATING_WINDOW];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var body : Object = notification.getBody();
			switch(notification.getName())
			{
				case ApplicationConstants.SHOW_CREATING_WINDOW:
					var project : ProjectInfo = notification.getBody() as ProjectInfo;
					showCreateWindow(project);
					break;
				case ApplicationConstants.HIDE_CREATING_WINDOW:
					removeCreateWindow();
					break;
			}
		}
		
		private function showCreateWindow(project : ProjectInfo = null) : void
		{
			// Make sure only one about window is shown
			if (createWindow == null)
			{
				createWindow = createNewWindow(project);
				PopUpManager.addPopUp(createWindow, application, true);
			}
			
			PopUpManager.bringToFront(createWindow);
			PopUpManager.centerPopUp(createWindow);
		}
		
		private function createNewWindow(project : ProjectInfo = null) : CreateWindow
		{
			var window : CreateWindow = new CreateWindow();
			
			if (project != null)
			{
				window.canvasWidth = project.canvasWidth;
				window.canvasHeight = project.canvasHeight;
				window.bgColor = project.bgColor;
			}
			
			window.addEventListener(CloseEvent.CLOSE, windowCloseListener);
			window.addEventListener(CreateDialogEvent.CREATE, createEventListener);
			window.addEventListener(DialogEvent.CANCEL, cancelEventListener);
			
			return window;
		}
		
		private function windowCloseListener(event : CloseEvent) : void
		{
			if (createWindow == event.target)
			{
				createWindow.removeEventListener(CloseEvent.CLOSE, windowCloseListener);
				
				sendCancel();
			}
			else
			{
				sendNotification(ApplicationConstants.GENERAL_ERROR);
			}
		}
		
		private function createEventListener(event : CreateDialogEvent) : void
		{
			if (createWindow == event.target)
			{
				createWindow.removeEventListener(CreateDialogEvent.CREATE, createEventListener);
				
				var project : ProjectInfo = new ProjectInfo();
				project.bgColor = createWindow.bgColor;
				project.canvasHeight = createWindow.canvasHeight;
				project.canvasWidth = createWindow.canvasWidth;
				
				sendCreate(project);
			}
			else
			{
				sendNotification(ApplicationConstants.GENERAL_ERROR);
			}
		}
		
		private function cancelEventListener(event : DialogEvent) : void
		{
			if (createWindow == event.target)
			{
				createWindow.removeEventListener(DialogEvent.CANCEL, cancelEventListener);
				sendCancel();
			}
			else
			{
				sendNotification(ApplicationConstants.GENERAL_ERROR);
			}
		}
		
		protected function sendCreate(project : ProjectInfo) : void
		{
			sendNotification(ApplicationConstants.CREATE, project);
		}
		
		protected function sendCancel() : void
		{
			sendNotification(ApplicationConstants.CANCEL_CREATING_REQUEST);
		}
		
		protected function removeCreateWindow() : void
		{
			if (createWindow != null)
			{
				if (createWindow.hasEventListener(CloseEvent.CLOSE))
				{
					createWindow.removeEventListener(CloseEvent.CLOSE, windowCloseListener);
				}
				if (createWindow.hasEventListener(DialogEvent.CANCEL))
				{
					createWindow.removeEventListener(DialogEvent.CANCEL, cancelEventListener);
				}
				if (createWindow.hasEventListener(CreateDialogEvent.CREATE))
				{
					createWindow.removeEventListener(CreateDialogEvent.CREATE, createEventListener);
				}
				PopUpManager.removePopUp(createWindow);
				createWindow = null;
			}
		}
	}
}